package csu.ssd.student;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.Optional;

import static csu.ssd.student.ShouyeController.scoreStage;

public class SignInController {

    @FXML
    private TextField password;

    @FXML
    private CheckBox men;

    @FXML
    private Button submit;

    @FXML
    private TextField email;
    @FXML
    private Label mailcheck;

    @FXML
    private Label check;

    @FXML
    private TextField name;

    @FXML
    private TextField id;
    @FXML
    private Button back;

    @FXML
    private TextField checkpassword;

    @FXML
    private CheckBox women;
    @FXML
    private ImageView right;

    @FXML
    private ImageView wrong;
    @FXML
    private Button clear;

        @FXML
    void checkpassword(ActionEvent event) {
            if (password.getText().trim().equals(checkpassword.getText().trim()))
            {
                check.setVisible(false);
            }
            else
            {
               check.setVisible(true);
            }
            submit.requestFocus();
    }
    @FXML
    void submitAction(ActionEvent event) {
            boolean sex1 = men.isSelected();
            boolean sex2 = women.isSelected();
            boolean wrong = false;
            if (sex1 && sex2){wrong=true;}

        if (password.getText().trim().equals(checkpassword.getText().trim())&&wrong==false){
        Alert alert1 = new Alert(Alert.AlertType.INFORMATION);
        alert1.setTitle("已提交");
        alert1.setHeaderText("提交成功");
        Optional<ButtonType> result = alert1.showAndWait();
        if (result.get() == ButtonType.OK){
            // ... user chose OK
            alert1.close();
            //否则
        } else {
            event.consume();
        }
        }
        else {
            Alert alert1 = new Alert(Alert.AlertType.INFORMATION);
            alert1.setTitle("已提交");
            alert1.setHeaderText("提交失败，请检查内容");
            Optional<ButtonType> result = alert1.showAndWait();
            if (result.get() == ButtonType.OK){
                // ... user chose OK
                alert1.close();
                //否则
            } else {
                event.consume();
            }

        }

    }


    public void iddone(ActionEvent event) {email.requestFocus();
    }

    public void namedone(ActionEvent event) {
            id.requestFocus();
    }

    public void backAction(ActionEvent event) {
        Stage stage =(Stage)  back.getScene().getWindow();
        stage.close();


        AnchorPane borderPane = null;
        try {
            borderPane = FXMLLoader.load(this.getClass().getResource("denglu.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //Stage stage = new Stage();
        Scene scene = new Scene(borderPane);

        scoreStage.setScene(scene);
        scoreStage.setTitle("登录页面");
        scoreStage.show();
    }

    public void clearall(ActionEvent event) {
        name.clear();
        id.clear();
        email.clear();
        password.clear();
        checkpassword.clear();
        men.setIndeterminate(false);
        women.setIndeterminate(false);
        men.setSelected(false);
        women.setSelected(false);
    }

    public void emaildone(ActionEvent event) {
        String emailPattern = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$";
        if (email.getText().matches(emailPattern)) {
            mailcheck.setVisible(false);
        } else {
            mailcheck.setVisible(true);

        }

            password.requestFocus();
    }

    public void passworddone(ActionEvent event) {checkpassword.requestFocus();
    }

    public void menclicked(ActionEvent event) {
            women.setIndeterminate(true);
    }

    public void womenclicked(ActionEvent event) {
            men.setIndeterminate(true);
    }
}
